<?php
$assetsUrl = Yii::app()->getModule('qa')->assetsUrl;
$cs = Yii::app()->clientScript;
$cs->registerCssFile("$assetsUrl/css/typography.css");
?>

<div class="form full">

	<?php
	$properties = array(
		'id'=>'qa-answer-form',
		'enableAjaxValidation'=>true,
	);
	$form = $this->beginWidget('CActiveForm', $properties);
	echo $form->hiddenField($model, 'parentId');
	?>

	<?php echo $form->errorSummary($model); ?>

	<table class="collapse">
		<tr>
			<td class="top">
				<div class="row">
					<div class="column">
						<?php
						$default = Yii::app()->request->hostInfo;
						$default .= $assetsUrl;
						$default .= '/images/gravatar-110.png';
						$userClass = Yii::app()->getModule('qa')->userClass;
						$userModel = call_user_func(array($userClass, 'model'));
						$user = $userModel->findByPk(Yii::app()->user->id);
						$firstname = CHtml::encode($user->profile->firstname);
						$config = array(
							'email'=>$user->email,
							'hashed'=>false,
							'default'=>$default,
							'size'=>110,
							'rating'=>'PG',
							'htmlOptions'=>array(
								'alt'=>"$firstname Avatar",
								'title'=>$firstname,
								'class'=>'round stroke',
							),
						);
						$this->widget('qa.widgets.GravatarWidget', $config);
						?>
						<div class="center">
							<?php echo $firstname; ?>
						</div>
					</div>
				</div>
			</td>
			<td class="full top">
				<div class="row">
					<?php
					$htmlOptions = array(
						'rows'=>15,
						'cols'=>50,
						'style'=>'font: 1em Menlo, Consolas, Courier, mono;',
						);
					echo $form->textArea($model, 'value', $htmlOptions);
					?>
					<?php echo $form->error($model, 'value'); ?>
				</div>

				<div class="right">
					<?php
					$url = 'http://daringfireball.net/projects/markdown/syntax';
					$htmlOptions = array(
						'target'=>'_blank',
					);
					echo CHtml::link('MarkDown Help', $url, $htmlOptions);
					?>
				</div>

				<div class="row buttons">
					<?php
					$isNew = $model->isNewRecord;
					echo CHtml::submitButton($isNew ? 'Submit' : 'Save');
					$htmlOptions = array('id'=>'preview');
					echo CHtml::button('Preview', $htmlOptions);
					?>
				</div>
				
				<div class="clear"></div>
				
			</td>
		</tr>
	</table>

	<?php $this->endWidget(); ?>

</div><!-- form -->

<?php
$properties = array(
	'id'=>'answer-preview',
	'options'=>array(
		'title'=>'Preview',
		'width'=>'800',
		'height'=>'300',
		'autoOpen'=>false,
		'resizable'=>true,
		'modal'=>true,
		'hide'=>'blind',
		'show'=>'blind',
	),
);
$this->beginWidget('zii.widgets.jui.CJuiDialog', $properties);
$view = 'application.modules.qa.views.answer._preview';
echo $this->renderPartial($view);
$this->endWidget('zii.widgets.jui.CJuiDialog');

$data = array(
	':valueId'=>CHtml::activeId($model, 'value'),
);
$cs->registerScriptContent(
	'_form', 'application.modules.qa.views.answer._form',
	CClientScript::POS_READY, $data
);
$assetsUrl = Yii::app()->getModule('qa')->assetsUrl;
$cs->registerScriptFile("$assetsUrl/js/Markdown.Converter.js");
?>